package com.hp.java09_springboot.controller;

import com.hp.java09_springboot.pojo.JsonResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
public class UploadController {

    public static final String UPLOAD_DIR = "D:\\apache-tomcat-8.5.61\\webapps\\upload\\";

    @ResponseBody
    @PostMapping("/upload")
    public JsonResult upload(MultipartFile file) throws IOException {
        String filename = file.getOriginalFilename();
        filename = UUID.randomUUID().toString().replace("-","")+
                filename.substring(filename.lastIndexOf("."));
        file.transferTo(new File(UPLOAD_DIR + filename));
        return new JsonResult(1,"ok",filename);
    }
}
